package com.example.appsnight;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import shared.Constants;
import android.os.AsyncTask;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import android.support.v4.app.NavUtils;

public class DownloadManager extends Activity {
	private String requestedURL;
	
	private class DownloadWebPageTask extends AsyncTask<String, Integer, String> {
		
		private String readBMAFeed(String url){
	    	StringBuilder builder = new StringBuilder();
	    	HttpClient client = new DefaultHttpClient();
	    	HttpGet httpGet = new HttpGet(url);
	    	try{
	    		HttpResponse response = client.execute(httpGet);
	    		StatusLine statusLine = response.getStatusLine();
	    		int statusCode = statusLine.getStatusCode();
	    	    if(statusCode == 200){
	    	    	HttpEntity entity = response.getEntity();
	    	    	InputStream content = entity.getContent();
	    	    	BufferedReader reader = new BufferedReader(new InputStreamReader(content));
	    	    	String line;
	    	    	while((line = reader.readLine()) != null ){
	    	    		builder.append(line);
	    	    	}
	    	    	int a = 1;
	    	    }else{
	    	    	showToast("Error while downloading information");
	    	    }
	    	}
	    	catch(ClientProtocolException e){
	    		e.printStackTrace();
	    	}
	    	catch(IOException e){
	    		e.printStackTrace();
	    	}
	    	return builder.toString();
	    }
		
		@Override
		protected String doInBackground(String... urls) {
			return readBMAFeed(urls[0]);
		}

		@Override
		protected void onPostExecute(String result) {
			showToast("DONE");
			if (requestedURL.equals(Constants.URL_ASSISTENTS)) startAssistentsActivity(result);
			else if (requestedURL.equals(Constants.URL_PROGRAM)) startProgramActivity(result);
		}

		@Override
		protected void onProgressUpdate(Integer... values) {
			showToast(values[0]+"%");
		}
	}

	public void showToast(String string) {
		int duration = Toast.LENGTH_SHORT;
		Toast toast = Toast.makeText(this, string, duration);
		toast.show();
	}

	public void startAssistentsActivity(String assistents) {
		Intent i = new Intent(this, Assistents.class);
		i.putExtra(Constants.EXTRA_ASSISTENTS, assistents);
		startActivity(i);
	}
	
	public void startProgramActivity(String programa) {
		Intent i = new Intent(this, Programa.class);
		i.putExtra(Constants.EXTRA_PROGRAM, programa);
		startActivity(i);
	}
	
    @SuppressLint({ "NewApi", "NewApi" })
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_download_manager);
        getActionBar().setDisplayHomeAsUpEnabled(true);
        
        Intent intent = getIntent();
        String activity = intent.getStringExtra(Constants.EXTRA_ACTIVITY);
      
        DownloadWebPageTask task = new DownloadWebPageTask();
      
        if (activity.equals(Constants.ASSISTENTS_ACTIVITY)) {
        	this.requestedURL = Constants.URL_ASSISTENTS;
        	task.execute(Constants.URL_ASSISTENTS);
        }
        else if (activity.equals(Constants.PROGRAM_ACTIVITY)) {
        	this.requestedURL = Constants.URL_PROGRAM;
        	task.execute(Constants.URL_PROGRAM);
        }
        else if (activity.equals(Constants.PASS_ACTIVITY)) {
        	this.requestedURL = Constants.URL_PASS;
        	task.execute(Constants.URL_PASS);
        }
        else showToast("No activity was initialized");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_download_manager, menu);
        return true;
    }

    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

}
